<template>
  <Container>
    <div class="container-header title title-center" slot="header">
      规则参数设置
    </div>
    <div class="bg-light pb">
      <div class="container-title">活跃门店设置</div>
      <Form :model="form" :label-width="340" ref="form" :rules="rules">
          <FormItem label="活跃门店为月度内抢购并扫码入库匹配数量大于等于：" prop="scanQuantity">
            <InputNumber :min="0" v-model="form.scanQuantity" :precision="0" style="width:200px"></InputNumber> 条
          </FormItem>
        </Form>
    </div>
    <div class="bg-light pb">
      <div class="container-title">基金限时设置</div>
      <Form :model="form" :label-width="166" ref="form" :rules="rules">
        <FormItem label="门店APP申请时间(天)：" prop="applyDay">
          每月 <InputNumber :max="31" :precision="0" :min="1" v-model="form.applyDay"></InputNumber> 日前
        </FormItem>
        <FormItem label="业务通评分时间：" prop="ratingDay">
          次月 <InputNumber :max="31" :precision="0" :min="1" v-model="form.ratingDay"></InputNumber> 日前
        </FormItem>
      </Form>
    </div>
    <div class="bg-light pb">
      <div class="container-title">经销商积分有效期设置</div>
      <Form :model="form" :label-width="170" ref="form" :rules="rules">
        <FormItem label="经销商积分有效期设置:" prop="fieldValue">
          <InputNumber :max="9999" :min="1" :precision="0" v-model="form.fieldValue" style="width:200px"></InputNumber> 天
        </FormItem>
      </Form>
    </div>
    <div class="bg-light pb">
      <div class="container-title">排行榜科室设置</div>
      <Button type="primary" @click="rankOfficeSet" class="ml20">选择科室</Button>
    </div>
    <div class="bg-light pb">
      <div class="container-title">独立团队政策设置</div>
      <Form :model="form" :label-width="70" ref="form" :rules="rules">
        <FormItem label="周期:" prop="day">
          <InputNumber :max="999999" :min="1" :precision="0" v-model="form.day"></InputNumber> 天
        </FormItem>
      </Form>
    </div>
    <div class="bg-light pb special-bg-light">
      <div class="container-title">巡店设置  <span class="font-blue ml">拜访最短时长 <InputNumber style="width:120px" placeholder="请输入最短时长" :precision="1" v-model="form.minute" :min="0"/>（分钟）</span></div>
      <Table
          stripe
          ref="selection"
          :columns="list.columns"
          :data="list.data"
          no-data-text="亲，没有找到相关记录哦！~"></Table>
    </div>
    <div class="bg-light pb">
      <div class="container-title">云店APP门店基金申请时提醒文案</div>
      <Form :model="form" :label-width="70" ref="form">
        <FormItem label="文案内容:">
          <Input v-model="form.remark" type="textarea" :rows="4" :maxlength="200" placeholder="云店APP门店基金申请时提醒文案" />
        </FormItem>
      </Form>
    </div>
    <Loader :status="status" @refresh="getData"></Loader>
    <PartolEditItem ref="edit" @complete="getPatrol"></PartolEditItem>
    <rankOfficeSet ref="rankOfficeSet"></rankOfficeSet>
    <div slot="footer" class="container-footer bg-white">
      <Button :disabled="disabled" @click="onSave" type="primary" size="large">确定</Button>
    </div>
  </Container>
</template>
<script>
import PatrolListData from './list/PatrolListData'
import PartolEditItem from './list/PartolEditItem'
import rankOfficeSet from "./list/rankOfficeSetEditItem";
export default {
  components: {
    PartolEditItem,
    rankOfficeSet
  },
  data() {
    return {
      tab: "tab1",
      status: "pass",
      disabled: false,
      list: PatrolListData,
      form: {
        scanQuantity: 0,
        fieldValue: "",
        limitId: "",
        validId: '',
        fieldKey: "",
        supplierId: "",
        applyDay: '',
        ratingDay: '',
        day: null,
        minute: null,
        remark: ''
      },
      rules: {
        scanQuantity: [{ required: true, message: '数字不能为空', type: 'number', trigger: 'change' }],
        fieldValue: [{required: true,message: "时间不能为空",type: "number",trigger: "change"}],
        applyDay: [{ required: true, message: '门店APP申请时间不能为空', type: 'number', trigger: 'change' }],
        ratingDay: [{ required: true, message: '业务通评时间不能为空', type: 'number', trigger: 'change' }],
        day: [{required: true, type: 'number', message: '周期不能为空',trigger: 'blur'}]
      }
    };
  },
  mounted() {
    this.$bus.$on('onEdit', row => this.onEdit(row))
    this.getData()
  },
  methods: {
    getScan() { // 活跃门店设置
      this.status = 'loading'
      this.$ajaxGet("/salesman/config/find_scan_quantity").then(data => {
        this.form.scanQuantity = data || 0
        this.status = 'pass'
      }).catch(() => {
        this.status = 'error'
      })
    },
    async onSaveScan() {
      let param = {
        scanQuantity: this.form.scanQuantity
      }
      await this.$ajaxPost("/salesman/config/update_scan_quantity", param)
    },
    getlimit() { // 基金限时设置
      this.status = 'loading'
      this.$ajaxGet('/setting/fund_time_limit/detail').then(data => {
        this.form.limitId = data.id
        this.form.applyDay = data.applyDay
        this.form.ratingDay = data.ratingDay
        this.status = 'pass'
      }).catch(() => {
        this.status = 'error'
      })
    },
    async onSaveLimit() {
      let param = {
        id: this.form.limitId,
        applyDay: this.form.applyDay,
        ratingDay: this.form.ratingDay
      }
      await this.$jsonPost("/setting/fund_time_limit/save", param)
    },
    getValid() { // 经销商积分有效期设置
      let params = {
        type: 1
      }
      this.$ajaxGet("/distributor/IntegralDictionary/get", params).then(res => {
          this.form.fieldKey = res.fieldKey
          this.form.validId = res.id
          this.form.fieldValue = res.fieldValue
          this.form.supplierId = res.supplierId
          this.status = "pass";
        }).catch(() => {
          this.status = "error";
        });
    },
    async onSaveValid() {
      let param = {
        fieldKey: this.form.fieldKey,
        fieldValue: this.form.fieldValue,
        id: this.form.validId,
        supplierId: this.form.supplierId
      }
      await this.$jsonPost('/distributor/IntegralDictionary/modify', param)
    },
    getDay() { // 独立团队政策设置
      this.status = 'loading'
      this.$ajaxGet('/salesman/config/find_day').then(res => {
        this.form.day = Number(res) || null
        this.status = 'pass'
      }).catch(err => {
        this.status = 'error'
      })
    },
    async onSaveDay() {
      let param = {
        day: this.form.day
      }
      await this.$ajaxPost('/salesman/config/update_day', param)
    },
    rankOfficeSet() {
      this.$refs.rankOfficeSet.openModal();
    },
    getMinute() {
      this.$ajaxGet('/salesman/config/inspection/duration/search').then(data => {
        this.form.minute = Number(data)
      })
    },
    getPatrol() { // 巡店设置
      this.$ajaxGet('/setting/salesman_inspection/list').then(res => {
        res.map(item => {
          let rolesArray = []
          if (item.roles.length) {
            item.roles.forEach(child => {
              rolesArray.push(child.desc)
            })
          }
          item.roleStrs = rolesArray.join(',')
        })
        this.list.data = res
        this.status = 'pass'
      }).catch(() => {
        this.status = 'error'
      })
    },
    async onSavePatrol() {
      let params = {
        minute: this.form.minute
      }
      await this.$ajaxPost('/salesman/config/inspection/duration/set', params)
    },
    getContent() {
      this.$ajaxGet('/setting/store_fund_tips/find').then(res => {
        this.form.remark = res.content
      })
    },
    async onSaveContent() {
      let param = {
        content: this.form.remark
      }
      await this.$ajaxPost('/setting/store_fund_tips/save', param)
    },
    onEdit(row) {
      this.$refs.edit.open(row)
    },
    getData() {
      this.getScan()
      this.getlimit()
      this.getValid()
      this.getDay()
      this.getMinute()
      this.getPatrol()
      this.getContent()
    },
    async allSave() {
      await this.onSaveScan()
      await this.onSaveLimit()
      await this.onSaveValid()
      await this.onSaveDay()
      await this.onSavePatrol()
      await this.onSaveContent()
    },
    onSave() {
      if (this.disabled) return;
      this.disabled = true
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.allSave().then(() => {
            this.disabled = false
            this.$Notice.success({
              title: `成功`
            })
          }).catch(() => {
            this.disabled = false
          })
        } else {
          this.disabled = false
        }
      })
    }
  },
  destroyed() {
    this.$bus.$off()
  }
};
</script>